package com.jun.admin.core.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.jun.admin.core.listener.JunStudentNewListener;
import com.jun.admin.core.mapper.JunStudentMapper;
import com.jun.admin.core.model.JunStudent;
import com.jun.admin.core.service.ExcelService;
import com.jun.admin.core.utils.ExcelUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author yuejun.li
 * @date 2024/10/8 10:08:12
 */
@Slf4j
@Service
@AllArgsConstructor
public class ExcelServiceImpl implements ExcelService {

    private JunStudentMapper junStudentMapper;

    @Override
    public void excelImport(MultipartFile file) throws IOException {
        ExcelUtil.excelImport(file,JunStudent.class,new JunStudentNewListener(this));
    }

    @Override
    public void excelExport(HttpServletResponse response) throws UnsupportedEncodingException {
        List<JunStudent> junStudents = junStudentMapper.selectAll();
        String fileName = "列表导出";
        ExcelUtil.excelExport(response,JunStudent.class,fileName,junStudents);
    }

    @Override
    public void excelFill(HttpServletResponse response) throws IOException {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template".concat("/").concat("user.xlsx"));

        String fileName = "列表导出";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName + ".xlsx", "UTF-8"));

        ExcelWriter excelWriter = null;
        try {
            excelWriter = EasyExcel
                    .write(response.getOutputStream())
                    .withTemplate(inputStream)
                    .build();

            // 这里 会填充到第一个sheet， 然后文件流会自动关闭
            List<JunStudent> junStudents = junStudentMapper.selectAll();

            //填充配置
            FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
            //填充
            excelWriter.fill(new FillWrapper("studuent1", junStudents),fillConfig,EasyExcel.writerSheet("sheet").build());
            excelWriter.fill(new FillWrapper("studuent2", junStudents),fillConfig,EasyExcel.writerSheet("sheet").build());
        }catch (IOException e) {
            log.error("export excel error error list:{}", e);
        } finally {
            //一定要关掉
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
    }

    @Override
    public boolean saveBatch(List<JunStudent> data) {
        return junStudentMapper.insertList(data) > 0;
    }
}
